/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <jni.h>

#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG,__VA_ARGS__)
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#include <android/log.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */


jstring stringFromJNI1( JNIEnv* env, jobject thiz )
{
    return env->NewStringUTF("Hello from JNI !");
}


jstring stringFromJNI2( JNIEnv* env,
                       jobject thiz,
                         jstring data )
{
    return env->NewStringUTF("another implemention for hello");
}

void call_java_method(JNIEnv* env, jobject thiz){
    jclass clazz;
    jmethodID mid;
    
    clazz = env->FindClass("com/example/jnidemo/JniDemo");
    if(clazz == NULL) return ;
      
    mid = env->GetMethodID(clazz, "viewSetText", "(I)V");
    
    if(mid == 0) return;
      
    env->CallVoidMethod(clazz, mid, 1);
}

static const JNINativeMethod gMethods[] = {
	{"stringFromJNI",     "()Ljava/lang/String;", 	(void*)stringFromJNI1},
	{"stringFromJNI",   "(Ljava/lang/String;)Ljava/lang/String;", 	(void*)stringFromJNI2},
  {"callJavaFromJNI", "()V", (void*)call_java_method},
};

static int registerMethods(JNIEnv* env) {
    static const char* const kClassName =
        "com/example/jnidemo/JniDemo";
    jclass clazz;

    /* look up the class */
    clazz = env->FindClass(kClassName);
    if (clazz == NULL) {
        return -1;
    }

    /* register all the methods */
    if (env->RegisterNatives(clazz, gMethods,
            sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
    {
        return -1;
    }

    /* fill out the rest of the ID cache */
    return 0;
}


// ----------------------------------------------------------------------------

/*
 * This is called by the VM when the shared library is first loaded.
 */
extern "C"  jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        goto bail;
    }
 
    if (registerMethods(env) != 0) {
       goto bail;
    }

    /* success -- return valid version number */
    result = JNI_VERSION_1_4;

bail:
    return result;
}
